package com.cskaoyan.service;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketGoodsExample;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxTopicDetailVo;
import com.cskaoyan.bean.vo.WxTopicGoodsVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author yi
 * @date 2022/10/14 14:37
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<MarketTopic> queryTopic(BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);
        return CommonData.data(marketTopicPageInfo);
    }

    @Override
    public WxTopicDetailVo queryTopicDetail(Integer id) {
        // 根据id查找获得Topic的详细信息
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        String[] goodsIds = marketTopic.getGoods();

        // 根据Topic里的goods的id，获得goods的list
        LinkedList<WxTopicGoodsVo> wxTopicGoodsVos = new LinkedList<>();
        for (String goodsId : goodsIds) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
            if (marketGoods.getDeleted()) {
                continue;
            }
            WxTopicGoodsVo wxTopicGoodsVo = TopicGoodsToVo(marketGoods);
            wxTopicGoodsVos.add(wxTopicGoodsVo);
        }

        // 写入vo
        WxTopicDetailVo wxTopicDetailVo = new WxTopicDetailVo();
        wxTopicDetailVo.setTopic(marketTopic);
        wxTopicDetailVo.setGoods(wxTopicGoodsVos);

        return wxTopicDetailVo;
    }

    private WxTopicGoodsVo TopicGoodsToVo(MarketGoods marketGoods) {
        WxTopicGoodsVo wxTopicGoodsVo = new WxTopicGoodsVo();
        wxTopicGoodsVo.setId(marketGoods.getId());
        wxTopicGoodsVo.setName(marketGoods.getName());
        wxTopicGoodsVo.setBrief(marketGoods.getBrief());
        wxTopicGoodsVo.setPicUrl(marketGoods.getPicUrl());
        wxTopicGoodsVo.setIsNew(marketGoods.getIsNew());
        wxTopicGoodsVo.setIsHot(marketGoods.getIsHot());
        wxTopicGoodsVo.setCounterPrice(marketGoods.getCounterPrice());
        wxTopicGoodsVo.setRetailPrice(marketGoods.getRetailPrice());
        return wxTopicGoodsVo;
    }

    @Override
    public CommonData<MarketTopic> queryTopicRelated(Integer id) {
        // 根据当前专题id查询该专题详细信息
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);


        // 获得该专题goods的id
        // List<Integer> goodsIdList  = new LinkedList<>();
        // for (String good : goods) {
        //     goodsIdList.add(Integer.parseInt(good));
        // }

        // 不做这个，太变态了
        // 获得该专题goods的catagory的id
        // Set<Integer> catagoryIdSet = new HashSet<>();
        // for (Integer goodsId : goodsIdList) {
        //     Integer catagoryId = marketGoodsMapper.selectByPrimaryKey(goodsId).getCategoryId();
        //     catagoryIdSet.add(catagoryId);
        // }

        // 获得当前专题goods列表
        String[] goodsIds = marketTopic.getGoods();
        Integer page = 1;
        Integer limit = 4;
        PageHelper.startPage(page, limit);
        LinkedList<MarketTopic> relatedTopics = new LinkedList<>();

        // 遍历topic表，找到其他拥有和该专题相同goods的专题，要排除掉自身专题
        for (String goodsId : goodsIds) {
            MarketTopicExample marketTopicExample = new MarketTopicExample();
            MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
            criteria.andIdNotEqualTo(id);
            criteria.andGoodsLike("%" + goodsId + "%");
            criteria.andDeletedEqualTo(false);

            List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
            for (MarketTopic topic : marketTopics) {
                relatedTopics.add(topic);
            }
        }





        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(relatedTopics);

        return CommonData.data(marketTopicPageInfo);
    }
}
